<?php
namespace ac\modules\member\controllers;

use Yii;
use ac\controllers\BasicController;
use yii\web\Controller;
use app\modules\member\models\Member;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;

class SetController extends BasicController
{
    protected $memberInfo;//登录信息

    public function init()
    {
        parent::init();
        if(isset($this->session->get('memberInfo')->loginname)){
            $this->memberInfo = $this->session->get('memberInfo');
        };
    }


    /*获取单个会员数据*/
    public function actionUserInfo()
    {
        // $this->memberInfo = (object)array(
        //     'member_id' => 24
        // );
        if(isset($this->memberInfo)){
            $member = Member::find()->select(['memberid', 'loginname', 'phone', 'email', 'nickname', 'sex', 'signature', 'headimgurl', 'curr_integral', 'cost_integral', 'curr_coupons', 'cost_coupons', 'unread_messages', 'comid', 'appid', 'add_time'])
                        ->where('member_id = :id', [':id' => $this->memberInfo->member_id])
                        ->asArray()->one();
            // P($member);
            if(!is_null($member)){
                return Tool::showRes([0, '获取成功'], $member);
            }
        }else{
            list($errno, $errmsg) = ErrMap::getError(10007);
            return Tool::showRes([$errno, $errmsg]);
        }
    }

	/*获取会员信息 此方法不再使用 之前是子应用调用的 保留下*/
	public function actionGetInfo()
	{
		if(Yii::$app->request->isPost and
            isset($this->post['member_id'])){
            // P($this->post);
            // return Tool::showRes([0, '获取成功'], $this->post);
            if(is_array($this->post)){//如果是 数组 的话，说明是取出 很多 会员数据
            	$member = Member::find()->select(['member_id', 'memberid', 'loginname', 'phone', 'email', 'nickname', 'sex', 'signature', 'headimgurl', 'comid', 'appid', 'add_time'])
            			->where(['in', 'member_id', $this->post['member_id']])
            			->asArray()->all();
            }
            if(is_numeric($this->post)){//如果是 数字 的话，说明是取出 单个 会员数据
            	$member = Member::find()->select(['member_id', 'memberid', 'loginname', 'phone', 'email', 'nickname', 'sex', 'signature', 'headimgurl', 'comid', 'appid', 'add_time'])
            			->where('member_id = :id', [':id' => $this->post['member_id']])
            			->asArray()->one();
            }
           	if(!is_null($member)){
           		return Tool::showRes([0, '获取成功'], $member);
           	}
        };
        
        return Tool::showRes(ErrMap::getError(10005));
	}

    /*个人设置*/
	public function actionModInfo()
	{
	    if(Yii::$app->request->isPost and
            isset($this->post['Member']['member_id']) and
            isset($this->post['Member']['appid'])){
        	/*根据appid 查询comid*/


            // P($this->post);
            $member = new Member;
            if($member->modifyMember($this->post, $this->post['Member']['member_id'])){
                $backInfo = [0, '修改会员成功'];//返回信息
            }else{
                if($member->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $member->getErrors();//异常信息
                    // P($exception);
                    if(array_key_exists("repeatLoginname", $exception)){
                        $backInfo = ErrMap::getError(10011);//返回信息
                    };
                    if(array_key_exists("repeatPhone", $exception)){
                        $backInfo = ErrMap::getError(10014);//返回信息
                    };
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
	}


	/*单独修改密码*/
	public function actionModPassword()
	{
        // P($this->post);
		if(Yii::$app->request->isPost 
            and isset($this->post['Member']['oldpassword'])
            and isset($this->post['Member']['appid'])
            and isset($this->post['Member']['comid'])){

            // P($this->post);
            $member = new Member;
            $info = $member::find()->where('member_id = :id', [':id' => $this->post['Member']['member_id']])->one();
            // P($info);
            if($info->password !== md5(md5($this->post['Member']['oldpassword']).$this->post['Member']['comid'])){
                $backInfo = ErrMap::getError(10024);//返回信息
            }else{
                if($member->modifyMember($this->post, $this->post['Member']['member_id'])){
                    $backInfo = [0, '修改密码成功'];//返回信息
                }else{
                    if($member->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $member->getErrors();//异常信息
                    }
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
	}

}
